//
//  FFPasswordFreePayVM.m
//  LLWallet
//
//  Created by tt on 2020/10/14.
//

#import "FFPasswordFreePayVM.h"
#import "FFWalletManager.h"

@interface FFPasswordFreePayVM ()
@property(nonatomic,strong) NSArray<FFPasswordFreeItem*> *items;
@end

@implementation FFPasswordFreePayVM


- (NSArray<NSDictionary *> *)tableSectionMaps {
    if(!self.items || self.items.count<=0) {
        return nil;
    }
    __weak typeof(self) weakSelf = self;
    NSMutableArray *items = [NSMutableArray array];
    for (FFPasswordFreeItem *item in self.items) {
        [items addObject:@{
            @"height":@(0.01),
            @"remark": item.desc?:@"",
            @"items": @[@{
                            @"class":WKSwitchItemModel.class,
                            @"label": item.title,
                            @"on":@(item.open),
                            @"onSwitch":^(BOOL on){
                                if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_passwordFreePay:item:on:)]) {
                                    [weakSelf.delegate ff_passwordFreePay:weakSelf item:item.item on:on];
                                }
                            }
            }],
        }];
    }
    return items;
}


// 免密支付设置
-(AnyPromise*) ff_passwordFreePay:(NSString*)item action:(NSString*)action payToken:(NSString*)payToken {
    return [[WKAPIClient sharedClient] POST:[NSString stringWithFormat:@"impay/wallet/passwordfree/%@",action] parameters:@{
        @"item": item,
        @"pay_token": payToken?:@"",
    }];
}

- (void)requestData:(void (^)(NSError * _Nullable))complete {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:@"impay/wallet/passwordfree/items" parameters:@{} model:FFPasswordFreeItem.class].then(^(NSArray<FFPasswordFreeItem*> *items){
        weakSelf.items = items;
        complete(nil);
    }).catch(^(NSError *error){
        complete(error);
    });
}

@end

@implementation FFPasswordFreeItem

+(WKModel*) fromMap:(NSDictionary*)dictory type:(ModelMapType)type {
    FFPasswordFreeItem *model = FFPasswordFreeItem.new;
    model.title = dictory[@"title"]?:@"";
    model.item = dictory[@"item"];
    model.desc = dictory[@"desc"];
    model.leftNum = [dictory[@"left_num"] integerValue];
    model.open = [dictory[@"open"] boolValue];
    return model;
}

@end
